<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
 * @author miaodb
 * 2011-4-21
 * 过滤商品描述信息中的外部链接，description_bak为备份字段，仅运行一次
 */
class CLI_Filter extends CLI
{
	public function main()
	{
		$bench_start = microtime(TRUE);

		$select = DB::select('goods_id', 'description')->from('goods');

		$iterator = new Db_Iterator($select, 'default', 10);

		$total = count($iterator);

		while ($iterator->valid())
		{
			foreach ($iterator->current() as $row)
			{
				$data = array(
					'description_bak'=>$row['description'],
					'description'=>$this->clearurl($row['description'])
				);

				DB::update('goods')->set($data)->where('goods_id', '=', $row['goods_id'])->execute();
				echo '>';
			}

			$iterator->next();
		}

		$bench_end = microtime(TRUE);

		$eclipse_time = round($bench_end - $bench_start, 3);

		//插入日志数据
		$log = __CLASS__." 共过滤 {$total} 条,执行用时  {$eclipse_time} 秒";
	}

	public function clearurl($str)
	{
		return preg_replace_callback( "/(?<=href=)[^>]*(?=\>)/i",array($this,"fiterurl"),$str);
	}


	public function fiterurl($matches)
	{
		$str=$matches[0];//每次的匹配

		$pos = strpos($str, "360hqb.com");//判断匹配是否是本站的链接

		if(!$pos)                                               //作相应的处理
		{
			//非本站链接替换
			return "#";
		}
		else
		{
			//本站链接不做处理
			return $str;
		}
	}
}